package xyz.zdsoft.ask_for_leave_manage_module.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import xyz.zdsoft.ask_for_leave_manage_module.bo.LeaveBo;
import xyz.zdsoft.ask_for_leave_manage_module.vo.CurStuLeaveBaseInfoVo;
import xyz.zdsoft.ask_for_leave_manage_module.vo.LeaveRecords;
import xyz.zdsoft.ask_for_leave_manage_module.vo.LeaveVo;
import xyz.zdsoft.ask_for_leave_manage_module.vo.StuLeaveDetailVo;
import xyz.zdsoft.common.entity.LeaveOperationRecord;
import xyz.zdsoft.common.utils.Result;

import java.util.List;

@Mapper
public interface StudentLeavesMapper extends BaseMapper<LeaveVo> {

    /**
     * 查询学生请假的基本信息
     * @param stuId 学生id
     * @return 基本信息
     */
    @Select("select distinct stu_name, user_no, colleges_name, major_name,\n" +
            "    grade_name, class_name, tea_name\n" +
            "from leave_info_view \n" +
            "where stu_id = #{stuId}")
    CurStuLeaveBaseInfoVo queryCurStuBaseInfo(@Param("stuId") Integer stuId);

    /**
     * 查询请假不通过的原因
     * @param leaveId 请假id
     * @return 不通过原因
     */
    @Select("select lor.reasons_failure \n" +
            "from leave_operation_record lor\n" +
            "where lor.leave_id=#{leaveId}\n" +
            "ORDER BY lor.operation_time desc \n" +
            "limit 1")
    String queryFailReasonByLeaId(@Param("leaveId") Integer leaveId);

    /**
     * 查询当前学生的请假记录
     * @param stuId 学生id
     * @return 总记录
     */
    @Select("SELECT\n" +
            "\tl.id,\n" +
            "\tsu.`name`,\n" +
            "\tlor.`status`,\n" +
            "\toperation_time,\n" +
            "\treasons_failure \n" +
            "FROM\n" +
            "\tleave_operation_record lor\n" +
            "\tleft JOIN `leave` l ON lor.leave_id = l.id\n" +
            "\tleft JOIN sys_user su ON l.student_id = su.id \n" +
            "WHERE\n" +
            "\tsu.id = #{stuId} \n" +
            "\tAND l.id = #{leaveId}")
    List<LeaveRecords> queryCurStuLeaveRecords(@Param("stuId") Integer stuId,
                                               @Param("leaveId") Integer leaveId);

    /**
     * 查询当前请假记录的附件
     * @param leaveId 请假id
     * @return 附件
     */
    @Select("SELECT id,file_name,file_url\n" +
            "FROM attachement\n" +
            "WHERE origin_id=#{leaveId}\n" +
            "AND origin_type = 2")
    List<StuLeaveDetailVo> queryStuLeaveAttach(@Param("leaveId") Integer leaveId);

    /**
     * 插入日志记录
     * @param record 日志记录
     * @return 是否成功
     */
    @Select({"<script>",
            "INSERT INTO leave_operation_record \n" +
            "(leave_id,`status`,operation_time" +
                    "<if test=\"record.reasonsFailure != null\">,reasons_failure</if>) \n" +
            "VALUES (#{record.leaveId},#{record.status},#{record.operationTime}" +
                    "<if test=\"record.reasonsFailure != null\">,#{record.reasonsFailure}</if>)",
            "</script>"})
    Integer insertLeaveLog(@Param("record") LeaveOperationRecord record);
}
